<template>
    <el-container class="home-container">
        <el-header>
            <div>
                <span><a @click="backHome">后台管理系统</a></span>
            </div>
            <span style="font-size: 15px">欢迎您：{{ username }}</span>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>

        <el-container>
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleClapse">||||</div>
                <el-menu
                        background-color="#333744"
                        text-color="#fff"
                        active-text-color="#ffd04b"
                        router
                        unique-opened
                        :collapse="isCollapse"
                        :collapse-transition="false"
                >
                    <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
                        <template slot="title">
                            <i class="el-icon-location"></i>
                            <span>{{item.name}}</span>
                        </template>
                        <el-menu-item
                                :index="subMenu.url+''"
                                v-for="subMenu in item.children"
                                :key="subMenu.id"
                        >
                            <template slot="title">
                                <i class="el-icon-location"></i>
                                <span>{{subMenu.name}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <!--显示菜单路由 对用的 组件 -->
                <router-view v-if="isRouterAlive" />
            </el-main>
        </el-container>
    </el-container>
</template>
<script>
    export default {
        data() {
            return {
                menuList: [],
                isCollapse: false,
                isRouterAlive: true,
                username:'',
            };
        },
        methods: {
          backHome(){
            this.$router.push({
              path:'/Home'
            })
          },
            reload() {
                this.isRouterAlive = false;
                this.$nextTick(function() {
                    this.isRouterAlive = true;
                });
            },
            toggleClapse() {
                this.isCollapse = !this.isCollapse;
            },
            logout() {
              window.localStorage.clear();
              this.$router.push("/login");
            },

            //初始化加载菜单  先查询所有菜单
            loadMenus() {
                let id = localStorage.getItem("id");

                let url = "/permission/showMenuByManager/"+id ;
                this.$axios.get(url)
                    .then(res=>{
                        if (res.data.code == 200){
                          console.log(res);
                          this.menuList = res.data.data;
                        }else {
                            this.$message({
                                type:'error',
                                message:res.data.msg,
                                duration:1000
                            })
                        }
                    })
            },
            // initUser() {
            //   let strUser = localStorage.getItem("managerInfo");
            //   let user = JSON.parse(strUser);
            //   this.username = user.name;
            // }
        },
        created() {
            this.loadMenus();
            // this.initUser();
        }
    };
</script>
<style  scoped>

    .toggle-button {
        background-color: #373d48;
        font-size: 10px;
        text-align: center;
        color: #fff;
        letter-spacing: 0.2em;
        cursor: pointer;
    }

    .home-container {
        height: 100%;
    }
</style>